<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" customClass="customWidth" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="100px" label-position="left">
			<el-form-item label="排序号" prop="sortNo">
				<el-input-number v-model="form.sortNo" placeholder="排序号" clearable></el-input-number>
			</el-form-item>
			<el-form-item label="试卷名称" prop="title">
				<el-input v-model="form.title" placeholder="试卷名称" clearable maxlength="80"></el-input>
			</el-form-item>
			<div>
				<el-form-item label="试题列表" prop="list">
					<sc-form-table v-model="questionsList" :addTemplate="addTemplate" drag-sort placeholder="暂无数据">
						<el-table-column prop="recordId" label="唯一标识" width="200" :show-overflow-tooltip="true">
							<template #default="scope">
								{{ scope.row.recordId }}
							</template>
						</el-table-column>
						<el-table-column prop="examType" label="题试类型" width="100">
							<template #default="scope">
								<el-tag v-if="scope.row.examType=='0'">单选</el-tag>
								<el-tag v-if="scope.row.examType=='1'">多选</el-tag>
								<el-tag v-if="scope.row.examType=='2'">简述</el-tag>
							</template>
						</el-table-column>
						<el-table-column prop="childTitle" label="试题题干">
							<template #default="scope">
								{{ scope.row.title }}
							</template>
						</el-table-column>
						<el-table-column prop="questionsList" label="子项">
							<template #default="scope">
								<div v-for="(item,i) in scope.row.questionsList" :key="i">
									({{ i + 1 }})
									{{ item.optValue }}、{{ item.childTitle }}
								</div>
							</template>
						</el-table-column>
						<el-table-column prop="grade" label="分值" width="100">
							<template #default="scope">
								{{ scope.row.grade }}/分
							</template>
						</el-table-column>
						<el-table-column prop="grade" label="操作" width="100">
							<template #default="scope">
								<el-button text type="primary" size="small" @click="setQuestions(scope.row, scope.$index)">选择</el-button>
							</template>
						</el-table-column>
					</sc-form-table>
				</el-form-item>
			</div>
			<el-form-item label="备注">
				<el-input v-model="form.remark" placeholder="备注" clearable type="textarea" maxlength="350"></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false">取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
	<selectQuestion v-if="dialog.save" ref="saveDialog" @closed="dialog.save=false"></selectQuestion>
</template>
<script>
import selectQuestion from "./selectQuestion"

export default {
	emits: ['success', 'closed'],
	components: {
		selectQuestion
	},
	data() {
		return {
			dialog: {
				save: false,
			},
			questionsList: [],
			addTemplate: {
				recordId: "",
				examType: '',
				title: '',
				grade: 0,
				questionsList: [],
			},
			visible: false,
			isSaveing: false,
			mode: "add",
			titleMap: {
				add: '创建试卷',
				edit: '试卷编辑',
				show: '查看'
			},
			form: {
				recordId: '',
				title: '',
				questionsList: '',
				sortNo: '0',
				remark: '',
			},
			rules: {
				title: [
					{required: true, message: '试卷名称不能为空', trigger: 'blur'}
				]
			}
		}
	},
	mounted() {
	},
	methods: {
		setQuestions(row, index) {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open().setData(row);
			})
		},
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					this.form.questionsList = JSON.stringify(this.questionsList);
					var res;
					if (this.mode == "add") {
						res = await this.$API.office.exam.insertExamQuestionRecord.post(this.form);
					} else if (this.mode == "edit") {
						res = await this.$API.office.exam.updateExamQuestionRecord.post(this.form);
					}
					this.isSaveing = false;
					if (res.code == 200) {
						this.$emit('success')
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", {type: 'error'})
					}
				}
			})
		},
		setData(data) {
			if (this.mode == "add") {
				this.form.sortId = data.sortId
			} else {
				this.form.recordId = data.recordId
				this.form.title = data.title
				this.form.questionsList = data.questionsList
				this.form.sortNo = data.sortNo
				this.form.remark = data.remark
				if (data.questionsList != null) {
					this.questionsList = JSON.parse(data.questionsList);
				}
			}
		},
	}
}
</script>

<style>
.customWidth {
	width: 60%;
}
</style>
